#ifndef __TCPCONNECTION_HPP__
#define __TCPCONNECTION_HPP__

#include <string>
#include "BoundedBuffer.hpp"
#include "IP.hpp"

/**
 * Encapsulates a TCP connection
 */
class TCPConnection {

	private:
		int fd; /**< underlying file descriptor */
		IP remoteAddress;
		BoundedBuffer<1024> buffer;

		unsigned int readData();
		bool haveLine() const;
		void sendData(const char* data, unsigned int length);

		bool closed;
		bool nonBlocking;

	public:
		TCPConnection(int fd, const IP& remote);

		std::string getNextLine();

		/**
		 * returns remote IP address as a string
		 */
		std::string getRemoteStr() const {
			return remoteAddress.getStr();
		}

		/**
		 * sends given text including \n at the end
		 */
		void sendLine(const std::string& text);

		/**
		 * sends given text without \n at the end
		 */
		void sendLineNoLB(const std::string &String);

		bool isClosed() const;

		void close();
		void setNonBlocking();
		bool canReadData();
		bool canWriteData();

		int getFD() const {
			return fd;
		}

		~TCPConnection();

};
#endif

